package com.centaline.androidsalesblog.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.SparseIntArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.DrawableRequestBuilder;
import com.centaline.androidsalesblog.R;
import com.centaline.androidsalesblog.adapter.viewholder.FpActivityVH;
import com.centaline.androidsalesblog.adapter.viewholder.FpDataVH;
import com.centaline.androidsalesblog.adapter.viewholder.FpEstateVH;
import com.centaline.androidsalesblog.adapter.viewholder.FpNewVH;
import com.centaline.androidsalesblog.adapter.viewholder.FpRentVH;
import com.centaline.androidsalesblog.adapter.viewholder.FpSaleVH;
import com.centaline.androidsalesblog.iml.RecyclerViewItemOnClickListener;
import com.centaline.androidsalesblog.model.businissobject.HistoryBo;
import com.centaline.androidsalesblog.utils.DateUtil;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;

import ca.barrenechea.widget.recyclerview.decoration.StickyHeaderAdapter;

/**
 * 足迹item
 * <p>
 * Created by fusl1 on 2015/12/24.
 */
public class FootPrintAdapter extends RecyclerView.Adapter implements StickyHeaderAdapter<FpDataVH> {

    private static final int RENT = 1;
    private static final int ESF = 2;
    private static final int NEW = 3;
    private static final int ACT = 4;

    private int mode = 0;//模式 0 :普通; 1:编辑模式
    private RecyclerViewItemOnClickListener<HistoryBo> itemOnClickListener;
    private final LayoutInflater layoutInflater;
    private final DrawableRequestBuilder<String> requestBuilder;
    private final ArrayList<HistoryBo> list;
    private SparseIntArray sparseIntArray = new SparseIntArray();
    private ArrayList<HistoryBo> selectArrayList = new ArrayList<>();
    private String newPropImg;
    private ListChangeListener listChangeListener;
    private GregorianCalendar calendar = new GregorianCalendar();

    public FootPrintAdapter(Context context, DrawableRequestBuilder<String> requestBuilder) {
        layoutInflater = LayoutInflater.from(context);
        this.requestBuilder = requestBuilder;
        list = new ArrayList<>();
    }

    /**
     * 刷新列表
     */
    public void recyclerList(ArrayList<HistoryBo> historyBos) {
        sparseIntArray.clear();
        selectArrayList.clear();
        list.clear();
        list.addAll(historyBos);
        notifyDataSetChanged();
    }

    public void setItemOnClickListener(RecyclerViewItemOnClickListener<HistoryBo> itemOnClickListener) {
        this.itemOnClickListener = itemOnClickListener;
    }

    /**
     * 新房图片ip
     */
    public void setNewPropImg(String newPropImg) {
        this.newPropImg = newPropImg;
    }

    /**
     * 模式
     */
    public void setMode(int mode) {
        setMode(mode, true);
    }

    /**
     * 模式
     */
    public void setMode(int mode, boolean notify) {
        this.mode = mode;
        if (notify) {
            notifyDataSetChanged();
        }
    }

    public void setListChangeListener(ListChangeListener listChangeListener) {
        this.listChangeListener = listChangeListener;
    }

    /**
     * 全选
     */
    public void selectAll(boolean selectAll) {
        if (selectAll &&
                list.size() != selectArrayList.size()) {
            selectArrayList.clear();
            selectArrayList.addAll(list);
            notifyDataSetChanged();
        } else if (!selectAll &&
                list.size() == selectArrayList.size()) {
            selectArrayList.clear();
            notifyDataSetChanged();
        }
    }

    /**
     * 显示数据的列表
     */
    public ArrayList<HistoryBo> getList() {
        return list;
    }

    /**
     * 已选择的id列表
     */
    public ArrayList<HistoryBo> getSelectArrayList() {
        return selectArrayList;
    }

    private void click(HistoryBo historyBo) {
        if (selectArrayList.contains(historyBo)) {
            selectArrayList.remove(historyBo);
        } else {
            selectArrayList.add(historyBo);
        }
        notifyDataSetChanged();
        listChange(selectArrayList.size() == list.size(), selectArrayList.size() > 0);
    }

    private void listChange(boolean selectAll, boolean hasSelect) {
        if (listChangeListener != null) {
            listChangeListener.change(selectAll, hasSelect);
        }
    }

    @Override
    public int getItemViewType(int position) {
        return list.get(position).getType();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case RENT:
                return new FpRentVH(layoutInflater.inflate(R.layout.item_foot_print_rent, parent, false));
            case ESF:
                return new FpEstateVH(layoutInflater.inflate(R.layout.item_foot_print_estate, parent, false));
            case NEW:
                return new FpNewVH(layoutInflater.inflate(R.layout.item_foot_print_new, parent, false));
            case ACT:
                return new FpActivityVH(layoutInflater.inflate(R.layout.item_foot_print_activity, parent, false));
            default:
                return new FpSaleVH(layoutInflater.inflate(R.layout.item_foot_print_sale, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final HistoryBo historyBo = list.get(position);
        final boolean select = selectArrayList.contains(historyBo);
        switch (getItemViewType(position)) {
            case RENT:
                FpRentVH fpRentVH = (FpRentVH) holder;
                fpRentVH.load(requestBuilder, historyBo.getPostDo(), mode, select);
                break;
            case ESF:
                FpEstateVH fpEstateVH = (FpEstateVH) holder;
                fpEstateVH.load(requestBuilder, historyBo.getEsfEstateDo(), mode, select);
                break;
            case NEW:
                FpNewVH fpNewVH = (FpNewVH) holder;
                fpNewVH.load(requestBuilder, historyBo.getNewEstDo(), newPropImg, mode, select);
                break;
            case ACT:
                FpActivityVH fpActivityVH = (FpActivityVH) holder;
                fpActivityVH.load(requestBuilder, historyBo.getNewEstActDo(), newPropImg, mode, select);
                break;
            default:
                FpSaleVH fpSaleVH = (FpSaleVH) holder;
                fpSaleVH.load(requestBuilder, historyBo.getPostDo(), mode, select);
                break;
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (itemOnClickListener != null)
                    itemOnClickListener.onItemOnClick(historyBo, mode);
                if (mode == 1) {
                    click(historyBo);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public long getHeaderId(int position) {
        calendar.setTimeInMillis(list.get(position).getDate());
        int day = calendar.get(Calendar.DATE);
        if (sparseIntArray.indexOfValue(day) == -1) {
            sparseIntArray.put(position, day);
        }
        return day;
    }

    @Override
    public FpDataVH onCreateHeaderViewHolder(ViewGroup parent) {
        final View view = layoutInflater.inflate(R.layout.item_foot_print_date, parent, false);
        return new FpDataVH(view);
    }

    @Override
    public void onBindHeaderViewHolder(FpDataVH holder, int position) {
        holder.atv_date.setText(DateUtil.format(list.get(position).getDate(), DateUtil.FORMAT5));
    }

    public interface ListChangeListener {
        void change(boolean selectAll, boolean hasSelect);
    }
}
